<?php
/**
 * Created by JetBrains PhpStorm.
 * User: joedarkangel3
 * Date: 8/27/11
 * Time: 3:12 PM
 * To change this template use File | Settings | File Templates.
 */
 
class food_model_test extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('unit_test');
        $dsn = 'mysql://root:c3603896@localhost/unit_test_yun';
        $this->load->database($dsn);
        $this->load->model('food_model', 'food');
        $this->load->library('unit_test');
        $this->unit->use_strict(true);
    }

    public function index()
    {
        // $category_id, $en_name, $ch_name, $sequence, $is_spicy, $size1en_name, $size1ch_name,
        // $size1price, $size2en_name, $size2ch_name, $size2price, $size3en_name, $size3ch_name, $size3price

        // get food test
        $test_category_id = '1';
        $rows = rand(1,20);
        $foods = $this->food->get_food($test_category_id, $rows);
        $this->unit->run(count($foods), $rows, "get food test");

        // create and read test, v-valid, iv_valid, r-read, c-create, wo-without, wi-with
        $iv_c_test = $this->food->create($test_category_id, "", "",
            "abcd", 2, "", "", "abcd", "", "", "Defc", "", "", "ttd");
        $this->unit->run(count($iv_c_test['user']), 13, "invalid data fail miserably");

        $v_c_test1_wo_error = $this->food->create($test_category_id, "enname1", "chname1",
            "11-11", 1, "lunch", "Lunch", "4.5", "Dinner", "Dinner", "6.5", "combo", "combo", "8");
        $v_c_test1_id = $this->db->insert_id();
        $this->unit->run(count($v_c_test1_wo_error['user']), 0, "perfect food creation query");

        $v_c_test1_wi_error = $this->food->create($test_category_id, "enname1", "chname1",
            "33-33", 0, "lunch", "Lunch", "4.5", "Dinner", "Dinner", "as", "", "", "8");
        $v_c_test2_id = $this->db->insert_id();
        $this->unit->run(array_key_exists('note', $v_c_test1_wi_error), true, "perfect food creation query");

        $r_data1 = $this->food->read($v_c_test1_id);
        $r_data2 = $this->food->read($v_c_test2_id);

        $this->unit->run($r_data1['sequence'], '11-11', 'data 1 reading test');

        $this->unit->run($r_data2['sequence'], '33-33', 'data 2 reading test');

        // update test, above test already taken care food model's schema validation method, no need to recheck
        $iv_id_update = $this->food->update("no_where_to_find_id", $test_category_id, "", "",
            "abcd", 2, "", "", "abcd", "", "", "Defc", "", "", "ttd");
        $this->unit->run(array_key_exists('update_query', $iv_id_update['system']), true, "invalid id update query test");

        $duplicate_seq_update = $this->food->update($v_c_test2_id, $test_category_id, "enname1", "chname1",
            "11-11", 1, "lunch", "Lunch", "4.5", "Dinner", "Dinner", "6.5", "combo", "combo", "8");
        $this->unit->run(array_key_exists('update_action', $duplicate_seq_update['user']), true, "duplicate key fail update");

        // valid update with same sequence
        $this->food->update($v_c_test2_id, $test_category_id, "enname1", "chname1",
            "33-33", 1, "lunch", "Lunch", "4.5", "Dinner", "Dinner", "6.5", "combo", "combo", "8");
        $updated_food = $this->food->read($v_c_test2_id);
        $this->unit->run($updated_food['size3en_name'], 'combo', 'valid update test');

        // swap test
        $same_place_swap = $this->food->swap($v_c_test2_id, $v_c_test2_id);
        $this->unit->run(array_key_exists('swap_sql', $same_place_swap['user']), true, "same place swap test");

        $this->food->swap($v_c_test1_id, $v_c_test2_id);
        $food1 = $this->food->read($v_c_test1_id);
        $food2 = $this->food->read($v_c_test2_id);
        $this->unit->run('33-33', $food1['sequence'], "food 1 sequence after swap");
        $this->unit->run('11-11', $food2['sequence'], "food 2 sequence after swap");

        // delete test now
        $system_error_delete = $this->food->delete("no_where_to_find");
        $this->unit->run(array_key_exists('delete_query', $system_error_delete['system']), true, 'delete data in broken system');
        
        $this->food->delete($v_c_test1_id);
        $this->food->delete($v_c_test2_id);

        $gone_data1 = $this->food->read($v_c_test1_id);
        $gone_data2 = $this->food->read($v_c_test2_id);

        $this->unit->run(count($gone_data1), 0, 'data 1 deleted test');
        $this->unit->run(count($gone_data2), 0, 'data 2 deleted test');

        // report now
        $this->_report();
    }

    private function _report()
    {
        if($this->input->is_ajax_request())
        {
            $final_result = $this->unit->result();
            $passed = 0;
            foreach($final_result as $res)
            {
                if($res['Result'] === "Passed")
                {
                    $passed++;
                }
            }
            if($passed == count($final_result))
            {
                $msg = json_encode(array('result' => 'passed'));
                echo $msg;
            }
            else{
                $msg = json_encode(array('result' => 'failed'));
                echo $msg;
            }
        }
        else{
            echo $this->unit->report();
        }
    }

}